﻿using Ecommerce.Areas.Manager.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using Ecommerce.Common;

namespace Ecommerce.Areas.Home.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Home/Customer/

        private DatabaseContext db = new DatabaseContext();

        [EAuthorization(LoginPage = EConst.CUSTOMER_LOGIN_PATH)]
        public ActionResult Index()
        {

            int customerID = Int32.Parse(User.Identity.Name);
            var model = db.Orders.Where(x => x.CustomerID == customerID).OrderByDescending(x => x.OrderDate);

            return View(model);
        }

        [EAuthorization(LoginPage = EConst.CUSTOMER_LOGIN_PATH)]
        public ActionResult OrderDetail(string orderID)
        {

            int ID = 0;
            try
            {
                ID = Int32.Parse(orderID);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            var order = db.Orders.Find(ID);
            string[] prodID = order.ProductID.Split(',');
            List<int> prodLst = new List<int>();
            for (int i = 0; i < prodID.Length - 1; i++)
            {
                prodLst.Add(Int32.Parse(prodID[i]));
            }
            var model = db.Products.Where(x => prodLst.Contains(x.ProductID));

            return View(model);
        }

        public ActionResult Login(string checkout)
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(Customer c, string checkout)
        {
            ModelState.Remove("FullName");  // to pass validate???
            if (ModelState.IsValid)
            {
                Customer tmp = db.Customers.Where(x => x.Email == c.Email && x.Password == x.Password).FirstOrDefault();
                if (tmp != null)
                {

                    FormsAuthentication.SetAuthCookie(tmp.CustomerID.ToString(), false);

                    if ("True".Equals(checkout))
                    {
                        return RedirectToAction("Index", "Checkout");
                    }

                    return RedirectToAction("index", "Template");
                }
                else
                {
                    ViewBag.ErrMsg = "Email or password is incorrect!";
                    return View(c);
                }
            }
            return View(c);
        }

        public ActionResult Register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Register(Customer c, string checkout)
        {
            if (ModelState.IsValid)
            {
                db.Customers.Add(c);
                db.SaveChanges();
                Session["loginID"] = c.CustomerID;
                if ("True".Equals(checkout))
                {
                    return RedirectToAction("Index", "Checkout");
                }

                return RedirectToAction("index", "Template");
            }

            return View("Register", c);
        }
    }
}
